#ifndef WEB_HANDLER_H
#define WEB_HANDLER_H

#include <boost/weak_ptr.hpp>

#include "file_streamer.h"

#include "../event_handler.h"
#include "../listener.h"
#include "../singletone.h"
#include "../configurator.h"
#include "../model_send.h"
#include "../temperature.h"
#include "../play.h"
#include "../pause.h"
#include "../tv.h"
#include "../light.h"

#include "../apriori/apriori.h"

#include "title_adapter.h"
#include "title_dataset.h"

#include "data_miner.h"

#include "../model/model_class.h"

namespace handler {
	class web_handler;
}

namespace handler {
	class web_handler : public network::event_handler,
			    public network::listener {
	public:
		web_handler(boost::shared_ptr<network::i_socket> s,
			    boost::shared_ptr<network::i_socket> micro)
		: _sock(s), _controller(micro) {}
		virtual ~web_handler() {}

		virtual network::event_type get_type() const
		{
			return network::RECEIVE;
		}
		virtual boost::shared_ptr<const network::i_socket>
			get_socket() const {return _sock;}
		virtual void handle_event(boost::shared_ptr<network::event> n);
		/**
		 * get notified if the receiver gets disconnect...
		 */
		virtual void notify(const network::notifier *n);

		static boost::shared_ptr<handler::web_handler> create(
			boost::shared_ptr<network::i_socket> sock, 
			boost::shared_ptr<network::i_socket> control)
		{
			boost::shared_ptr<handler::web_handler> ptr(
				new handler::web_handler(sock, control));
			ptr->_this = ptr;
			return ptr;
		}

	private:
		void do_play(boost::shared_ptr<model::dom> node);
		void do_list(boost::shared_ptr<model::dom> node);
		void do_pause(boost::shared_ptr<model::dom> node);
		void do_temp(boost::shared_ptr<model::dom> node);
		void do_light(boost::shared_ptr<model::dom> node);
		void do_tv(boost::shared_ptr<model::dom> node);
		void do_random(const string &client, const string &file);

		void send_play(boost::shared_ptr<model::client>, 
			       boost::shared_ptr<model::title>);

		model::client* find_client(const string &name);
		model::title* find_title(const string &file, model::client *);

		boost::shared_ptr<network::i_socket>
			find_socket(const model::client *c) const;

		boost::shared_ptr<network::i_socket> _sock;
		boost::shared_ptr<network::i_socket> _controller; 

		boost::weak_ptr<handler::web_handler> _this;
	};
}

#endif
